package com.future.mayi.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.dialog.CommonDialog;
import com.future.mayi.model.RedPacketModel;
import com.future.mayi.utils.PriceUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RedPacketAdapter extends BaseRecyclerViewAdapter<RedPacketModel> {


    /**
     * type
     * 0未使用
     * 1已使用
     * 10已过期
     * 4领取
     */
    private int type;

    private int itemH;
    private int textW;


    private double money;

    public RedPacketAdapter(Context context, XRecyclerView xRecyclerView, int type) {
        super(context, null, xRecyclerView);
        this.type = type;
        itemH = (int) (CommonUtils.getScreenSize(context)[0] * 0.2689f) + CommonUtils.dip2px(context, 15);
        textW = (CommonUtils.getScreenSize(context)[0] - CommonUtils.dip2px(context, 30)) / CommonUtils.px2dip(mContext, 344) * CommonUtils.px2dip(mContext, type == 4 ? 70 : 40);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (type == 1)
            return new ItemViewHolder(getView(parent, R.layout.layout_red_packet_use_item));
        else if (type == 2)
            return new ItemViewHolder(getView(parent, R.layout.layout_red_packet_select_item));
        else if (type == 10)
            return new ItemViewHolder(getView(parent, R.layout.layout_red_packet_old_item));
        else
            return new ItemViewHolder(getView(parent, R.layout.layout_red_packet_item));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ItemViewHolder holder = (ItemViewHolder) viewHolder;
        if (mOnItemClickListener != null)
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (type == 2) {
                        select(position);
                    }
                    mOnItemClickListener.OnItem(view, position);
                }
            });
        RedPacketModel model = getItem(position);
        holder.tvTitle.setText(model.getRedpack_name());
        holder.tvSub.setText("获取方式：" + model.getResources());
        holder.tvTime.setText("过期时间：" + model.getExpirytime());
        holder.tvNum.setText(PriceUtils.getPriceText(model.getPar_value()));
        if (type == 2)
            holder.ivSelect.setImageResource(model.isSelect() ? R.drawable.tab_selected : R.drawable.tab_normal);
    }


    private void select(int position) {
        List<RedPacketModel> list = getSelect();
        RedPacketModel model = getItem(position);
        if ((!model.isSelect()) && list != null && list.size() > 0 && money < 1000) {
            new CommonDialog.Builder(mContext)
                    .setMessage("订单商品金额大于1000才能选择多个红包使用哦！")
                    .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
            return;
        }


        if (model != null) {
            model.setSelect(!model.isSelect());
            notifyDataSetChanged();
        }

    }


    class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_sub)
        TextView tvSub;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.ll_item)
        LinearLayout llItem;
        ImageView ivSelect;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            if (type == 2)
                ivSelect = (ImageView) itemView.findViewById(R.id.iv_select);
        }
    }

    @Override
    public void setList(List<RedPacketModel> items) {
        super.setList(items);
    }

    public void setType(int type) {
        this.type = type;
    }


    public List<RedPacketModel> getSelect() {
        if (getItemCount() == 0) return null;
        List<RedPacketModel> select = new ArrayList<>();
        for (RedPacketModel redPacketModel : getList()) {
            if (redPacketModel != null && redPacketModel.isSelect() && redPacketModel.getPar_value() > 0) {
                select.add(redPacketModel);
            }
        }
        return select;
    }

    public void setMoney(double money) {
        this.money = money;
    }
}
